﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;

namespace Site_CamaraTibau
{
    public partial class Requerimetos : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    VereadorBO.Listar(ddlVereador);
                    pesquisar();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        #region Contexto
        ModeloContainer bco = new ModeloContainer();
        #endregion

        #region Funcoes
        public void pesquisar()
        {
            try
            {
                var objs = (from a in bco.Requerimentoes where a.id == a.id select a).ToList();

                if (ddlVereador.SelectedIndex > 0)
                {
                    int id = int.Parse(ddlVereador.SelectedValue);

                    objs = (from a in objs where a.idVereador == id select a).ToList();
                }


                if (!string.IsNullOrEmpty(txtPesquisarNumero.Text))
                {
                    objs = (from a in objs where a.Numero.Contains(txtPesquisarNumero.Text.Trim()) select a).ToList();
                }

                if (!string.IsNullOrEmpty(txtPesquisarAssunto.Text))
                {
                    objs = (from a in objs where a.Assuntto.ToLower().Contains(txtPesquisarAssunto.Text.ToLower().Trim()) select a).ToList();
                }

                if (!string.IsNullOrEmpty(txtPesquisarBeneficiado.Text))
                {
                    objs = (from a in objs where a.Beneficiado.ToLower().Contains(txtPesquisarBeneficiado.Text.ToLower().Trim()) select a).ToList();
                }
                
                if (!string.IsNullOrEmpty(txtPesquisarDtInicial.Text))
                {
                    DateTime dtInicial = DateTime.Parse(txtPesquisarDtInicial.Text);

                    objs = (from a in objs where a.dtEntrada.Date >= dtInicial.Date select a).ToList();

                }
                if (!string.IsNullOrEmpty(txtPesquisarDtFinal.Text))
                {
                    DateTime dtFinal = DateTime.Parse(txtPesquisarDtFinal.Text);

                    objs = (from a in objs where a.dtEntrada.Date <= dtFinal.Date select a).ToList();
                }


                var objs2 = (from a in objs
                             select new
                             {
                                 ID = a.id,
                                 Vereador = a.Vereador.nome + " (" + a.Vereador.Partido.sigla + ")",
                                 Numero = a.Numero,
                                 DtEntrada = a.dtEntrada,
                                 URL = "~/requerimento/" + a.URL
                             }).ToList();


                gvListagem.DataSource = objs2;
                gvListagem.DataBind();

                if (objs.Count == 1)
                    lblNumeroLinhas.Text = "Foi encontrado " + objs.Count + " registro.";
                else if (objs.Count > 1)
                    lblNumeroLinhas.Text = "Foram encontrados " + objs.Count + " registros.";
                else
                    lblNumeroLinhas.Text = "";

            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region Botoes
        protected void lbkVerBusca_Click(object sender, EventArgs e)
        {
            if (div_busca.Visible == true)
                div_busca.Visible = false;
            else
                div_busca.Visible = true;
        }
        protected void btnPesquisar_Click(object sender, EventArgs e)
        {
            pesquisar();
        }

        #endregion


        #region Grids

        protected void gvListagem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Contains("cmdVisualizar"))
            {
                string url = e.CommandArgument.ToString();

                PaginaBase.ResponseHelper.Redirect(url, "_blank", ",HEIGHT=620,WIDTH=820,top=50,right=50,toolbar=no,scrollbars=no,resizable=no");
            }
        }


        protected void gvListagem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListagem.PageIndex = e.NewPageIndex;
            pesquisar();
        }

        protected void gvListagem_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#BDD9F6'");
                // when mouse leaves the row, change the bg color to its original value   
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
            }
        }
        #endregion
    }
}